<template>
  <div class="phone-login">
    <van-nav-bar 
      title="手机号登录" 
      left-arrow 
      fixed 
      placeholder 
      @click-left="router.back()" 
      class="custom-nav"
    />
    <div class="login-content">
      <div class="login-header">
        <img src="@/assets/images/logo.png" alt="logo" class="logo">
        <h2 class="welcome">欢迎回来</h2>
        <p class="subtitle">请使用手机号登录</p>
      </div>

      <van-form @submit="onSubmit" ref="formRef">
        <div class="login-form">
          <div class="input-wrapper">
            <van-field name="phone" v-model="formData.phone" type="tel" placeholder="请输入手机号" class="custom-field"
              :rules="phoneRules" :border="false">
              <template #left-icon>
                <van-icon name="phone-o" />
              </template>
            </van-field>

            <van-field v-model="formData.sms" center name="sms" placeholder="请输入验证码" class="custom-field"
              :rules="codeRules" :border="false">
              <template #left-icon>
                <van-icon name="shield-o" />
              </template>
              <template #button>
                <van-button size="small" type="primary" class="code-btn"
                  :disabled="!!countdown || !formData.phone || !/^1[3-9]\d{9}$/.test(formData.phone)" @click="sendSms"
                  style="z-index: 1;">
                  {{ countdown ? `${countdown}s后重新获取` : '获取验证码' }}
                </van-button>
              </template>
            </van-field>
          </div>

          <van-button block round type="primary" native-type="submit" class="submit-btn" :loading="loading">
            登录
          </van-button>

          <div class="action-links">
            <span class="link" @click="router.push('/auth/register')">没有账号？去注册</span>
          </div>
        </div>
      </van-form>

      <div class="other-login">
        <div class="divider">其他登录方式</div>
        <div class="login-options">
          <van-button class="option-btn" round block plain @click="router.push('/auth/login')">
            账号密码登录
          </van-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onUnmounted } from 'vue'
import { useRouter } from 'vue-router'
import { showToast } from 'vant'
import { useUserStore } from '@/stores/user'
import { phoneRules, codeRules } from '@/utils/validate'
import { useCountdown } from '@/composables/useCountdown'

import type { FormInstance } from 'vant'


const router = useRouter()
const userStore = useUserStore()
const formRef = ref<FormInstance>()
const { countdown, startCountdown, stopCountdown } = useCountdown()
onUnmounted(() => {
  stopCountdown()
})

const formData = ref({
  phone: '13297556624',
  sms: ''
})

const loading = ref(false)

const sendSms = async () => {
  try {
    if (!formRef.value) return

    // 先验证手机号
    await formRef.value.validate('phone')

    // 发送验证码
    await userStore.sendSms(formData.value.phone)
    showToast('验证码已发送')
    startCountdown()
  } catch (error: any) {
    const message = error?.response?.data?.message || error?.message || '发送失败'
    showToast(message)
  }
}

const onSubmit = async () => {
  try {
    loading.value = true
    await userStore.phoneLogin({
      phone: formData.value.phone,
      sms: formData.value.sms
    })
    showToast('登录成功')
    router.push('/home')
  } catch (error: any) {
    showToast(error.message || '登录失败')
  } finally {
    loading.value = false
  }
}
</script>

<style lang="scss" scoped>
.phone-login {
  min-height: 100vh;
  background: linear-gradient(to bottom, #F8E6FF, #F0E6FF);

  .custom-nav {
    background: transparent;

    :deep(.van-nav-bar__title) {
      color: #333;
      font-size: 18px;
      font-weight: 500;
    }

    :deep(.van-icon) {
      color: #333;
      font-size: 20px;
    }
  }

  .login-content {
    padding: 50px 20px 32px;
  }

  .login-header {
    text-align: center;
    margin-bottom: 25px;

    .logo {
      width: 72px;
      height: 72px;
      border-radius: 16px;
      margin-bottom: 16px;
      box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    }

    .welcome {
      font-size: 22px;
      font-weight: 600;
      color: #333;
    }

    .subtitle {
      font-size: 14px;
      color: #666;
    }
  }

  .login-form {
    margin-bottom: 45px;

    .input-wrapper {
      background: #fff;
      border-radius: 12px;
      padding: 8px 16px;
      margin-bottom: 24px;
    }

    :deep(.custom-field) {
      background: transparent;
      padding: 5px 0;
      // height: 48px;
      // margin-bottom: 12px;
      border-bottom: 1px solid rgba(0, 0, 0, 0.08);

      .van-field__error-message {
        position: absolute;
        left: 0px; // 与输入框文本对齐
        bottom: -7px;
        font-size: 12px;
        // color: #ff4646; // 更柔和的红色
        white-space: nowrap; // 防止文字换行
      }

      // 输入框获得焦点时的样式
      &.van-field--error {
        .van-field__control {
          color: #ff4646;
        }

        .van-field__left-icon .van-icon {
          color: #ff4646;
        }
      }

      &:last-child {
        margin-bottom: 0;
        border-bottom: none;
      }

      .van-field__control {
        font-size: 16px;
        color: #333;
        height: 48px;
        padding-left: 8px;

        &::placeholder {
          color: #999;
          font-size: 15px;
        }
      }

      .van-field__left-icon {
        display: flex;
        align-items: center;
        margin-right: 4px;

        .van-icon {
          font-size: 20px;
          color: #666;
        }
      }

      .code-btn {
        height: 36px;
        padding: 0 16px;
        border-radius: 18px;
        background: $primary-color !important;
        border: none;
        font-size: 14px;
      }
    }

    .submit-btn {
      width: 100%;
      height: 48px;
      font-size: 15px; // 统一字号为15px
      font-weight: 500;
      background: #8A2BE2;
      border: none;
      margin: 30px 0 24px;
    }

    .action-links {
      display: flex;
      justify-content: space-between;
      padding: 0 4px;

      .link {
        color: #8A2BE2;
        font-size: 14px;
        cursor: pointer;
        opacity: 0.9;

        &:active {
          opacity: 0.7;
        }
      }
    }
  }

  .other-login {
    .divider {
      text-align: center;
      color: #666;
      font-size: 14px;
      margin-bottom: 16px;
      position: relative;

      &::before,
      &::after {
        content: '';
        position: absolute;
        top: 50%;
        width: 25%;
        height: 1px;
        background: rgba(0, 0, 0, 0.1);
      }

      &::before {
        left: 16px;
      }

      &::after {
        right: 16px;
      }
    }

    .login-options {
      display: flex;
      flex-direction: column;
      gap: 12px;
      padding: 0 4px;
    }

    .option-btn {
      width: 100%;
      height: 44px;
      background: rgba(255, 255, 255, 0.9);
      border: none;
      border-radius: 22px;
      color: #8A2BE2;
      font-size: 15px;
      cursor: pointer;
      transition: all 0.3s;
      box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);

      &:active {
        background: rgba(255, 255, 255, 0.7);
        transform: scale(0.98);
      }
    }
  }
}
</style>

